package com.abbas.smartCity.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.bean.inn.InnListBean;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 青年驿站
 */
public class InnExpendableAdapter extends BaseExpandableListAdapter {

    private final List<InnListBean.RowsBean> parentData;
    private final OnExpandClickListener listener;

    public InnExpendableAdapter(List<InnListBean.RowsBean> parentData, OnExpandClickListener listener) {
        this.parentData = parentData;
        this.listener = listener;
    }

    @Override
    public int getGroupCount() {
        return parentData == null ? 0 : parentData.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public InnListBean.RowsBean getGroup(int i) {
        return parentData.get(i);
    }

    @Override
    public String getChild(int i, int i1) {
        return parentData.get(i).getIntroduce();
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inn_parent_item, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.number = (TextView) convertView.findViewById(R.id.number);
            viewHolder.address = (TextView) convertView.findViewById(R.id.address);
            viewHolder.bt = (Button) convertView.findViewById(R.id.bt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final InnListBean.RowsBean group = getGroup(i);
        Glide.with(convertView.getContext())
                .load(App.getBaseUrl() + group.getCoverImgUrl())
                .into(viewHolder.im);
        viewHolder.name.setText(group.getName());
        viewHolder.number.setText("男床位数：" + group.getBedsCountBoy() + "\n女床位数：" + group.getBedsCountGirl());
        viewHolder.address.setText(group.getAddress());
        viewHolder.bt.setOnClickListener((v) -> listener.onClick(i, b));
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        final TextView textView = new TextView(viewGroup.getContext());
        textView.setText(getGroup(i).getIntroduce());
        return textView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public static class ViewHolder {
        ImageView im;
        TextView name;
        TextView number;
        TextView address;
        Button bt;
    }

    public interface OnExpandClickListener {
        void onClick(int i, Boolean b);
    }
}
